const express = require('express')
const { User } = require('../models/user')

const router = express.Router()

router.get('/list', async (req, res) => {
  const users = await User.findAll()
  User.someStaticMethod()
  if (users[0]) users[0].someInstanceMethod()
  res.send({ code: 0, msg: 'ok', data: users })
});

router.post('/', express.json(), async (req, res) => {
  const user = await User.create(req.body)
  res.send({ code: 0, msg: 'ok', data: user.toJSON() })
})

router.patch('/:id', express.json(), async (req, res) => {
  await User.update(req.body, { where: { id: +req.params.id } })
  res.send({ code: 0, msg: 'ok' })
})

router.delete('/:id', async (req, res) => {
  await User.destroy({ where: { id: +req.params.id } })
  res.send({ code: 0, msg: 'ok' })
})

module.exports = {
  router,
}
